package cn.tedu.blog.server.controller.system;

import cn.tedu.blog.server.pojo.dto.CategoryAddDTO;
import cn.tedu.blog.server.pojo.dto.CategoryAddNewDTO;
import cn.tedu.blog.server.pojo.vo.CategoryDetailVO;
import cn.tedu.blog.server.pojo.vo.CategoryListVO;
import cn.tedu.blog.server.service.ICategoryService;
import cn.tedu.blog.server.utils.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author blue
 * @description: 后台系统类别管理控制器
 * @date
 */
@Api(tags = "3. 类别管理")
@Slf4j
@RestController
@RequestMapping("/system/category")
public class CategoryController {


    @Autowired
    private ICategoryService categoryService;

    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody CategoryAddNewDTO categoryAddNewDTO){
        log.debug("接收到的请求参数：{}", categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除类别")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id){
        log.debug("接收到删除类别的请求");
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("批量删除")
    @ApiOperationSupport(order = 300)
    @PostMapping("/deleteByIds")
    public JsonResult deleteByIds(@RequestBody Long[] ids){
        log.debug("接受到批量删除业务的请求:");
        int rows = categoryService.deleteByIds(ids);
        String message="成功删除"+rows+"个分类";
        return JsonResult.ok(message);
    }

    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 400)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable Long id,@RequestBody CategoryAddDTO categoryAddDTO){
        log.info("接受到的请求参数: "+id);
        log.info("接受到的请求参数: "+categoryAddDTO);
        categoryService.updateById(id,categoryAddDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询")
    @ApiOperationSupport(order = 500)
    @GetMapping("/{id:[0-9]+}/selectById")
    public JsonResult select(@PathVariable Long id){
        CategoryDetailVO categoryDetailVO=categoryService.categorySelect(id);
        return JsonResult.ok(categoryDetailVO);
    }

    @ApiOperation("查询类别")
    @ApiOperationSupport(order = 600)
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询类别的请求");
        List<CategoryListVO> list=categoryService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("获取分类数据")
    @ApiOperationSupport(order = 700)
    @PostMapping("/countAll")
    public int getCountAll(@RequestBody CategoryListVO categoryListVO){
        log.debug("【countAll】接收到客户端提交的请求，参数：{}",categoryListVO);
        return categoryService.getCountAll(categoryListVO);
    }

    @ApiOperation("分页查询类别")
    @ApiOperationSupport(order = 800)
    @PostMapping("/listByCategory")
    public List<CategoryListVO> listCategory(@RequestBody CategoryListVO categoryListVO){
        log.debug("接收到客户端提交的条件分页查询类别请求，参数：{}",categoryListVO);
        return categoryService.listByCategory(categoryListVO);
    }

    @ApiOperation("查询当前分类下的文章数分类")
    @ApiOperationSupport(order = 900)
    @PostMapping("/ArtcleCount")
    public int artcleCount(@RequestBody Long id){
        log.debug("接受到客户端的查询文章量的请求:");
        return categoryService.articleCount(id);
    }
//
//

//
//    @GetMapping("/getBackFillById")
//    public CategoryListVO getBackFillById(Long id){
//        log.debug("接收到客户端提交的请求，参数：{}",id);
//        return categoryService.getBackFillById(id);
//    }

}